package com.weishaoying.practice.web;

import com.weishaoying.practice.entity.SysUser;
import com.weishaoying.practice.entity.UserDto;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeAction {

    @RequestMapping("/home")
    public String first(Model model) throws Exception {
        Subject subject = SecurityUtils.getSubject();
        System.out.println("subject = " + subject);
        SysUser sysUser = (SysUser) subject.getPrincipal();
        model.addAttribute("sysUser", sysUser);
        return "home";
    }

    @RequestMapping("/welcome")
    public String welcome(Model model) throws Exception {
        return "/welcome";
    }

    @RequestMapping("/student/query")
    public String queryStudents(Model model) throws Exception {
        System.out.println("-----------queryStudents = " + model);
        model.addAttribute("t", "queryStudents");
        return "biz";
    }

    @RequestMapping("/teacher/query")
    public String queryTeachers(Model model) throws Exception {
        System.out.println("----------queryTeachers = " + model);
        model.addAttribute("t", "queryTeachers");
        return "biz";
    }

    @RequestMapping("/book/query")
    public String queryBooks(Model model) throws Exception {
        //TODO:这里是如何知道当前用户的 private static final ThreadLocal<Map<Object, Object>> resources
        Subject subject = SecurityUtils.getSubject();
        System.out.println("------------subject = " + subject);
        boolean hasTeacher = subject.hasRole("teacher");
        System.out.println("hasTeacher = " + hasTeacher);
        boolean hasStudent = subject.hasRole("student");
        System.out.println("hasStudent = " + hasStudent);
        boolean hasAdmin = subject.hasRole("admin");
        System.out.println("hasAdmin = " + hasAdmin);
        boolean permitted = subject.isPermitted("book:edit");
        System.out.println("permitted = " + permitted);
        boolean permitted2 = subject.isPermitted("car:drive");
        System.out.println("permitted2 = " + permitted2);


        System.out.println("----------queryBooks = " + model);
        model.addAttribute("t", "queryBooks");
        return "biz";
    }

    @RequestMapping("/book/edit")
    public String editBook(Model model) throws Exception {
        System.out.println("----------editBook = " + model);
        model.addAttribute("t", "editBook");
        return "biz";
    }

    @RequestMapping("/book/delete")
    public String delBook(Model model) throws Exception {
        System.out.println("----------delBook = " + model);
        model.addAttribute("t", "delBook");
        return "biz";
    }
}	